package org.dromara.visitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.visitor.domain.dto.OrganizationAddReq;
import org.dromara.visitor.domain.dto.OrganizationResp;
import org.dromara.visitor.domain.entity.OrgManage;
import org.dromara.visitor.domain.entity.Organization;
import org.dromara.visitor.domain.entity.RuleOrg;
import org.dromara.visitor.mapper.OrgManageMapper;
import org.dromara.visitor.mapper.OrganizationMapper;
import org.dromara.visitor.mapper.RuleOrgMapper;
import org.dromara.visitor.service.OrganizationService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class OrganizationServiceImpl extends ServiceImpl<OrganizationMapper, Organization> implements OrganizationService {
    @Resource
    private OrganizationMapper organizationMapper;

    @Resource
    private OrgManageMapper orgManageMapper;

    @Resource
    private RuleOrgMapper ruleOrgMapper;
    @Override
    public void addOrg(OrganizationAddReq addReq) {
        LambdaQueryWrapper<Organization> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Organization::getIsFirst,1);
        Organization first = organizationMapper.selectOne(queryWrapper);
        Organization organization = new Organization();
        BeanUtils.copyProperties(addReq,organization);
        if(first == null){
            organization.setIsFirst(1);
        }
        //查询是否重名
        queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Organization::getOrgName,addReq.getOrgName());
        Organization exist = organizationMapper.selectOne(queryWrapper);
        if(exist != null){
            throw new ServiceException("组织名称已存在");
        }
        LoginUser loginUser = LoginHelper.getLoginUser();
        if(organization.getId() == null){
            organization.setCreateBy(loginUser.getUserId());
            organization.setCreateTime(LocalDateTime.now());
        }
        organization.setUpdateBy(loginUser.getUserId());
        organization.setUpdateTime(LocalDateTime.now());
        this.saveOrUpdate(organization);
    }

    @Override
    public List<OrganizationResp> queryList() {
        LambdaQueryWrapper<Organization> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Organization::getCreateTime);
        List<Organization> list = organizationMapper.selectList(queryWrapper);
        List<OrganizationResp> respList = list.stream().map(item -> {
            OrganizationResp resp = new OrganizationResp();
            BeanUtils.copyProperties(item,resp);
            return resp;
        }).toList();
        return respList;
    }

    @Override
    public void deleteOrg(Long id) {
      LambdaQueryWrapper<OrgManage> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(OrgManage::getOrgId,id);
      queryWrapper.last("limit 1");
      OrgManage orgManage = orgManageMapper.selectOne(queryWrapper);
      if(orgManage != null){
          throw new ServiceException("该组织下已有成员不能删除");
      }
      Organization organization = this.getById(id);
      if(organization.getIsFirst() == 1){
          throw new ServiceException("默认组织不能删除");
      }
      this.removeById(id);
    }

    @Override
    public List<OrganizationResp> unRuleList() {
        LambdaQueryWrapper<RuleOrg> ruleOrgQueryWrapper = new LambdaQueryWrapper<>();
        ruleOrgQueryWrapper.select(RuleOrg::getOrgId);
        List<RuleOrg> ruleOrgList = ruleOrgMapper.selectList(ruleOrgQueryWrapper);
        List<OrganizationResp> respList = new ArrayList<>();
        LambdaQueryWrapper<Organization> orgQueryWrapper = new LambdaQueryWrapper<>();
        if(CollectionUtils.isNotEmpty(ruleOrgList)){
            Set<Long> orgIdList = ruleOrgList.stream().map(RuleOrg::getOrgId).collect(Collectors.toSet());
            orgQueryWrapper.notIn(Organization::getId,orgIdList);
        }
        List<Organization> orgList = organizationMapper.selectList(orgQueryWrapper);
        if(CollectionUtils.isNotEmpty(orgList)){
            respList  = orgList.stream().map(item -> {
                OrganizationResp resp = new OrganizationResp();
                BeanUtils.copyProperties(item,resp);
                return resp;
            }).toList();
            return respList;
        }
        return respList;
    }
}
